package org.example.crop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.crop.domain.*;
import org.example.crop.service.CheckLogService;
import org.example.crop.service.StashCropService;
import org.example.crop.service.StashService;
import org.example.crop.service.StashSizeService;
import org.example.crop.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("stash")
public class StashController {
    @Autowired
    private StashService stashService;
    @Autowired
    private StashCropService stashCropService;
    @Autowired
    private StashSizeService stashSizeService;
    @Autowired
    private CheckLogService checkLogService;
    @RequestMapping("add")
    public Result add(@RequestBody Crop crop){
        boolean save = stashService.saveAll(crop);
        CheckLog checkLog = new CheckLog();
        checkLog.setCropId(crop.getId());
        checkLog.setStatus("入库");
        checkLog.setNum(crop.getNum());
        checkLog.setCreateTime(new Date());
        checkLogService.save(checkLog);
        return save?Result.success():Result.fail("规格不符合/剩余空间不足");
    }
    @RequestMapping("outStash")
    public Result outStash(@RequestBody StashCrop stashCrop){
        LambdaQueryWrapper<StashSize> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StashSize::getId,stashCrop.getStsId());
        StashSize stashSize = stashSizeService.getOne(wrapper);
        stashSize.setNum(stashSize.getNum()+stashCrop.getNum());
        stashSizeService.updateById(stashSize);




        LambdaQueryWrapper<StashCrop> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(StashCrop::getCropId,stashCrop.getCropId());
        StashSize one = stashSizeService.getOne(wrapper);



        if(one.getNum()<=stashCrop.getNum()){
            return Result.fail("库存不足");
        }
        boolean outStash=stashCropService.outStash(stashCrop);
        if(outStash){
            CheckLog checkLog = new CheckLog();
            checkLog.setCropId(stashCrop.getCropId());
            checkLog.setStatus("出库");
            checkLog.setNum(stashCrop.getNum());
            checkLog.setCreateTime(new Date());
            checkLog.setMarketId(stashCrop.getMarketId());
            checkLogService.save(checkLog);
        }

        return outStash?Result.success():Result.fail();
    }
    @RequestMapping("del")
    public void del(@RequestBody StashCrop stashCrop){
        StashCrop stashCrop1 = stashCropService.getListByCropId(stashCrop);
        if(stashCrop1.getNum()<=0){
            stashCropService.removeById(stashCrop1);
            StashCrop stashCrop2 = stashCropService.getListByCropId(stashCrop);
            stashCrop2.setNum(stashCrop2.getNum()+stashCrop1.getNum());
            stashCropService.updateById(stashCrop2);
        }
    }
}
